package com.liuyanzhao.blog.controller.Admin;


import com.liuyanzhao.blog.entity.Category;
import com.liuyanzhao.blog.entity.custom.CategoryCustom;

import com.liuyanzhao.blog.service.ArticleService;
import com.liuyanzhao.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 
 * @author Anbing Cui
 *  标签分类
 * @CrateTime: 2019年6月22日
 */
@Controller
@RequestMapping("/admin/category")
public class BackCategoryController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;
	/**
	 * 
	 *  查看全部标签
	 * @throws Exception
	 * @Return: ModelAndView
	 * @author Anbing Cui
	 * @CreateDate: 2019年6月22日 下午3:26:51
	 */
    @RequestMapping(value = "")
    public ModelAndView categoryList() throws Exception {
        ModelAndView modelandview = new ModelAndView();

        List<CategoryCustom> categoryCustomList = categoryService.listCategory(null);
        modelandview.addObject("categoryCustomList",categoryCustomList);

        modelandview.setViewName("Admin/Category/index");
        return modelandview;

    }
    /**
     * 
     *  添加新的标签
     * @param category  标签属性
     * @throws Exception
     * @Return: String  返回到全部标签页
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日
     */
    @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST)
    public String insertCategorySubmit(Category category) throws Exception {
        categoryService.insertCategory(category);
        return "redirect:/admin/category";
    }
    /**
     * 
     *  根据id删除分类,禁止删除有文章的分类
     * @param id  分类的ID
     * @throws Exception
     * @Return: String  返回到全部标签页
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日
     */
    @RequestMapping(value = "/delete/{id}")
    public String deleteCategory(@PathVariable("id") Integer id) throws Exception {
        //禁止删除有文章的分类
        int count = articleService.countArticleWithCategory(null,id);
        if (count == 0) {
            categoryService.deleteCategory(id);
        }
        return "redirect:/admin/category";
    }
    /**
     * 
     *  重新编辑分类前的查询,查询分类的详细信息
     * @param id  要编辑分类的ID
     * @throws Exception
     * @Return: ModelAndView  包含分类的信息(categoryCustom)和跳转路径(Admin/Category/edit)
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:27:06
     */
    @RequestMapping(value = "/edit/{id}")
    public ModelAndView editCategoryView(@PathVariable("id") Integer id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        CategoryCustom categoryCustom =  categoryService.getCategoryById(null,id);
        modelAndView.addObject("categoryCustom",categoryCustom);

        List<CategoryCustom> categoryCustomList = categoryService.listCategory(null);
        modelAndView.addObject("categoryCustomList",categoryCustomList);

        modelAndView.setViewName("Admin/Category/edit");
        return modelAndView;
    }
    /**
     *  更新标签信息
     * @param category
     * @throws Exception
     * @Return: String
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:27:12
     */
    @RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
    public String editCategorySubmit(Category category) throws Exception {
        categoryService.updateCategory(category);
        return "redirect:/admin/category";
    }
}
